{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Bucles\n",
    "\n",
    "Temas:\n",
    "1. bucles `while` \n",
    "2. bucles `for`\n",
    "<br>\n",
    "\n",
    "## bucles while\n",
    "\n",
    "La sintaxis para un `while` es\n",
    "\n",
    "```julia\n",
    "while *condición*\n",
    "    *cuerpo de bucle*\n",
    "end\n",
    "```\n",
    "\n",
    "Por ejemplo, usaremos un `while` para contar o iterar sobre un arreglo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n = 0\n",
    "while n < 10\n",
    "    n += 1\n",
    "    println(n)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myfriends = [\"Ted\", \"Robyn\", \"Barney\", \"Lily\", \"Marshall\"]\n",
    "\n",
    "i = 1\n",
    "while i <= length(myfriends)\n",
    "    friend = myfriends[i]\n",
    "    println(\"Hola $friend, ¡gusto en verte!\")\n",
    "    i += 1\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## bucles for\n",
    "\n",
    "La sintaxis para un bucle `for` es\n",
    "\n",
    "```julia\n",
    "for *var* in *iterable de bucle*\n",
    "    *cuerpo de bucle*\n",
    "end\n",
    "```\n",
    "\n",
    "Podemos usar un bucle for para generar los datos anteriores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for n in 1:10\n",
    "    println(n)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myfriends = [\"Ted\", \"Robyn\", \"Barney\", \"Lily\", \"Marshall\"]\n",
    "\n",
    "for friend in myfriends\n",
    "    println(\"Hi $friend, it's great to see you!\")\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Nota: se puede reemplazar `in` con `=` ó `∈`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for n = 1:10\n",
    "    println(n)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for n ∈ 1:10\n",
    "    println(n)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ahora usemos bucles `for` para crear tablas de sumas donde el valor de cada entrada es la suma de los índices del renglón y la columna. <br>\n",
    "\n",
    "Primero, inicializamos el arreglo con puros 0s."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "m, n = 5, 5\n",
    "A = fill(0, (m, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in 1:m\n",
    "    for j in 1:n\n",
    "        A[i, j] = i + j\n",
    "    end\n",
    "end\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Aquí va un poquito de azúcar sintáctica para el bucle `for`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "B = fill(0, (m, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in 1:m, j in 1:n\n",
    "    B[i, j] = i + j\n",
    "end\n",
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "La manera mas \"Juliana\" de crear esta tabla es por medio de un *array comprehension / comprehensión de arreglo*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "C = [i + j for i in 1:m, j in 1:n]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "En el próximo ejemplo, embebimos un arreglo de comprehensión en un bucle `for`, generando tablas de adición de tamaño creciente."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for n in 1:10\n",
    "   A = [i + j for i in 1:n, j in 1:n]\n",
    "   display(A)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ejercicios\n",
    "\n",
    "4.1 Crea un diccionario `squares`, que tiene llaves de valores de 1 a 100. El valor asociado a cada llave es el cuadrado de la llave. Guarda los valores asociados a las llaves pares como enteros y las impares como cadenas. Por ejemplo,\n",
    "\n",
    "```julia\n",
    "squares[10] == 100\n",
    "squares[11] == \"121\"\n",
    "```\n",
    "\n",
    "(¡No necesitas condicionales para esto!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "4.2 Usa `fill` para crea una matriz de `10x10` de solo `0`'s. Pobla las primeras 10 entradas con el índice de esa entrada. ¿Julia usa el orden de primero columna o primero renglón? (O sea, ¿el \"segundo\" elemento es el de la primera columna en el primer renglón, ó es el de el primer renglón en la segunda columna?)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.1",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
